<?php

namespace app\admin\controller;

use app\models\{Admin, Order as Orders, Reimburs, ReimbursLog,User};
use think\Db;
use think\Facade\Config as Configs;
use app\facade\Redis;


class Main extends Base
{

    public function welcome()
    {

        // $keys = Redis::keys('user_*');
        // foreach ($keys as $key) {
        //     $key = str_replace('dt_', '', $key);
        //     $user_id = Redis::get($key);
        //     $user = (new User())->where(['id' => $user_id,'type' => [4,5]])->find() ?? '';
        //     if(!empty($user)){
        //         $ttl = Redis::ttl($key);
        //         dump(date('Y-m-d H:i:s',time()-$ttl));
        //         dump($user);
        //     }
        // }
        // die;
        $admin = $this->request->admin;
        $groups = explode(',', $this->request->admin['groups']) ?? [];
        //上个月初
        $lastmonth = date('Y-m-01', strtotime('-1 month')) . ' 00:00:00';
        //上个月末
        $tlastmonth = date('Y-m-t', strtotime('-1 month')) . ' 23:59:59';
        //本月初时间
        $starmonth = date('Y-m-01') . ' 00:00:00';
        //本月当日
        $endmonth = date('Y-m-t') . ' 23:59:59';
        $model = new Reimburs();
        if ($admin['type'] == 3) {
            $results = $model;
            $data = [
                'havemoney' => $results->where(['groups' => $groups])->where('status', 2)->sum('total'), //累计已报销金额
                'notmoney' => $results->where(['groups' => $groups])->where('status', 1)->sum('total'), //累计待报销金额
                'tomonth' => $results->where(['groups' => $groups])->whereTime('created', 'between', [$starmonth, $endmonth])
                    ->where('status', 2)->sum('total'), //本月已报销金额
                'tonotmoney' => $results->where(['groups' => $groups])->where('status', 1)->sum('total'), //剩余待报销金额
                'yesmoney' => $results->where(['groups' => $groups])->whereTime('created', 'between', [$lastmonth, $tlastmonth])
                    ->where('status', 2)->sum('total'), //上月累计报销金额
                'lists' => [] //各消费明细计总  本月已报销  本月待报销  上月已报销  所有待报销
            ];
        } else {
            $results = $model;
            $data = [
                'havemoney' => $results->where('status', 2)->sum('total'), //累计已报销金额
                'notmoney' => $results->where('status', 1)->sum('total'), //累计待报销金额
                'tomonth' => $results->whereTime('created', 'between', [$starmonth, $endmonth])
                    ->where('status', 2)->sum('total'), //本月已报销金额
                'tonotmoney' => $results->where('status', 1)->sum('total'), //剩余待报销金额
                'yesmoney' => $results->whereTime('created', 'between', [$lastmonth, $tlastmonth])
                    ->where('status', 2)->sum('total'), //上月累计报销金额
                'lists' => [] //各消费明细计总  本月已报销  本月待报销  上月已报销  所有待报销
            ];
        }

        $reimburs_type = config()["config"]['reimburs_type'];
        $list = [];
        foreach ($reimburs_type as $k => $v) {
            $list[] = [
                'name' => $v,
                'money' => (new Reimburs())->where(['rl.type' => $k, 'r.status' => 2])->alias('r')->join('ReimbursLog rl', 'rl.reimbursid = r.id')->sum('money'),
                'havemoney' => (new Reimburs())->whereTime('r.created', 'between', [$starmonth, $endmonth])->where(['rl.type' => $k, 'r.status' => 2])->alias('r')->join('ReimbursLog rl', 'rl.reimbursid = r.id')->sum('money'),
                'notmoney' => (new Reimburs())->whereTime('r.created', 'between', [$starmonth, $endmonth])->where(['rl.type' => $k, 'r.status' => 1])->alias('r')->join('ReimbursLog rl', 'rl.reimbursid = r.id')->sum('money'),
                'yesmoney' => (new Reimburs())->whereTime('r.created', 'between', [$lastmonth, $tlastmonth])->where(['rl.type' => $k, 'r.status' => 2])->alias('r')->join('ReimbursLog rl', 'rl.reimbursid = r.id')->sum('money'),
                'notallmoney' => (new Reimburs())->where(['rl.type' => $k, 'r.status' => 1])->alias('r')->join('ReimbursLog rl', 'rl.reimbursid = r.id')->sum('money')
            ];
        }
        $data['lists'] = $list;
        //累计已报销金额
        return view(null, [
            'admin' => $admin,
            'data' => $data,
        ]);
    }

    public function index()
    {
        $admin = $this->request->admin;
        $menu = config('menu.menu');
        $arr = [];
        foreach ($menu as $k => $v) {
            if ($v['show'] == true) {
                foreach ($v['sub'] as $x => $y) {
                    if (($y['show'] == true && in_array($y['uri'], $admin['role'])) || $admin['is_default'] == 1) {
                        $arr[$k]['name'] = $v['name'];
                        $arr[$k]['ico'] = $v['ico'];
                        $arr[$k]['sub'][$x] = $y;
                    }
                }
            }
        }
        return view(null, [
            'admin' => $admin,
            'menu' => $arr,
        ]);
    }


    public function miss()
    {
        return view('404');
    }

    public function admin()
    {
        $admin = Admin::get(session('admin_id'));
        if ($this->request->isPost()) {
            if (empty($admin)) {
                return json(['status' => 1, 'msg' => '登陆已过期!']);
            }

            $data = input('post.');
            if ($data['password'] != '') {
                if ($data['password'] != $data['passworder']) {
                    return json(['status' => 1, 'info' => '密码不一致']);
                }
                $data['newpassword'] = $data['password'];
                $data1['password'] = md5(md5($data['password']) . md5($admin['entry']));
            }
            $data1['nickname'] = $data['nickname'];
            $data1['images'] = $data['images'];
            $data1['poster'] = $data['poster'];
            if ($admin->save($data1, ['id' => $admin->id])) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            return view('admin', ['admin' => $admin]);
        }
    }
}
